import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * User: 5
 * Date: 16-12-9
 * Time: 下午1:01
 * To change this template use File | Settings | File Templates.
 */
public class AtomicInt {

    @Test
    public void test() {
        AtomicInteger atomicInteger = new AtomicInteger(10);
        for (int i = 0; i < 10; i++) {
            new Thread(new A(atomicInteger)).start();
        }
        Assert.assertEquals(10, atomicInteger.get());
    }

    @Test
    public void test2() {
        AtomicInteger atomicInteger = new AtomicInteger(10);
        System.out.println(atomicInteger.getAndIncrement());   //==++
        System.out.println(atomicInteger.get());
    }
}

class A implements Runnable {
    private static AtomicInteger atomicInteger;

    A(AtomicInteger atomicInteger) {
        this.atomicInteger = atomicInteger;

    }

    @Override
    public void run() {
        atomicInteger.getAndIncrement();  //++
        atomicInteger.getAndDecrement();  //--
    }
}